package cn.tedu.carehome.foundation.controller;

import cn.tedu.carehome.foundation.pojo.dto.IllAddNewDTO;
import cn.tedu.carehome.foundation.pojo.dto.IllUpdateDTO;
import cn.tedu.carehome.foundation.pojo.dto.MedicineAddNewDTO;
import cn.tedu.carehome.foundation.pojo.dto.MedicineUpdateDTO;
import cn.tedu.carehome.foundation.pojo.vo.IllListItemVO;
import cn.tedu.carehome.foundation.pojo.vo.IllStandardVO;
import cn.tedu.carehome.foundation.pojo.vo.MedicineListItemVO;
import cn.tedu.carehome.foundation.pojo.vo.MedicineStandardVO;
import cn.tedu.carehome.foundation.restful.JsonPage;
import cn.tedu.carehome.foundation.restful.WebConsts;
import cn.tedu.carehome.foundation.service.IIllService;
import cn.tedu.carehome.foundation.service.IMedicineService;
import cn.tedu.carehome.foundation.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/ills")
@Slf4j
@Api(tags = "病例管理模块")
@Valid
public class IllController {
    @Autowired
    private IIllService illService;

    @PostMapping("/add-new")
    @ApiOperation("添加病例信息")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@RequestBody IllAddNewDTO illAddNewDTO) {
        log.debug("开始处理【添加老人健康信息】的请求，参数：{}", illAddNewDTO);
        illService.addNew(illAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除病例信息")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "病例ID", required = true, example = "3", dataType = "long")
    public JsonResult<Void> delete(@Range(min = 1, max = 1000000, message = "删除病例信息失败，病例id非法")
                                   @PathVariable Long id) {
        log.debug("开始处理【根据id删除病例信息】的请求，参数：{}", id);
        illService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改病例详情")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> updateInfoById(@RequestBody IllUpdateDTO illUpdateDTO) {
        log.debug("开始处理【修改老人病例详情信息】的请求，参数：{}", illUpdateDTO);
        illService.updateInfoById(illUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据id查询病例详情")
    @ApiOperationSupport(order = 410)
    public JsonResult<IllStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询老人健康详情】的请求，ID：{}", id);
        IllStandardVO illStandardVO = illService.getStandardById(id);
        return JsonResult.ok(illStandardVO);
    }

    @GetMapping("")
    @ApiOperation("查询病例信息列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<IllListItemVO>> list() {
        log.debug("开始处理【查询病例信息列表】的请求，无参数");
        List<IllListItemVO> list = illService.list();
        return JsonResult.ok(list);
    }

    @GetMapping("/list-by-page")
    @ApiOperation("分页查询病例信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码", name = "page", example = "1"),
            @ApiImplicitParam(value = "每页条数", name = "pageSize", example = "2")
    })
    public JsonResult<JsonPage<IllListItemVO>> listIllsByPage(
            @RequestParam(required = false, defaultValue = WebConsts.DEFAULT_PAGE) Integer page,
            @RequestParam(required = false, defaultValue = WebConsts.DEFAULT_PAGE_SIZE) Integer pageSize) {
        JsonPage<IllListItemVO> illsByPage = illService.listIllsByPage(page, pageSize);
        return JsonResult.ok(illsByPage);
    }
}
